<template>
  <div class="dialog-form">
    <el-form ref="postRef" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="岗位名称" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入岗位名称" />
      </el-form-item>
      <el-form-item label="岗位编码" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入编码名称" />
      </el-form-item>
      <el-form-item label="岗位顺序" prop="postSort">
          <el-input-number v-model="form.postSort" controls-position="right" :min="0" :step="1" :precision="0" />
      </el-form-item>
      <el-form-item label="岗位状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in sys_normal_disable"
                :key="dict.value"
                :value="dict.value"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onConfirm">确认</el-button>
    </div>
  </div>
</template>

<script setup>
import { addPost, getPost, updatePost } from "@/api/system/post"
const { proxy } = getCurrentInstance()
const { sys_normal_disable } = proxy.useDict("sys_normal_disable")

const emits = defineEmits(['cancel', 'confirm'])

const postRef = ref()
const form = ref({
  postId: undefined,
  postCode: undefined,
  postName: undefined,
  postSort: 0,
  status: "0",
  remark: undefined
})
const rules = {
  postName: [{ required: true, message: "岗位名称不能为空", trigger: "blur" }],
  postCode: [{ required: true, message: "岗位编码不能为空", trigger: "blur" }],
  postSort: [{ required: true, message: "岗位顺序不能为空", trigger: "blur" }],
}

const pageInit = (postId) => {
  if (postId) {
    getPost(postId).then(response => {
      form.value = response.data
    })
  }
}

const onCancel = () => {
  emits('cancel')
}

const onConfirm = () => {
  postRef.value.validate(valid => {
    if (valid) {
      if (form.value.postId) {
        updatePost(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          postRef.value.resetFields()
          emits('confirm')
        });
      } else {
        addPost(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          postRef.value.resetFields()
          emits('confirm')
        })
      }
    }
  })
}

defineExpose({
  pageInit
})

</script>

<style lang="scss" scoped>

</style>